<?php

namespace sil29\VitrineBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use sil29\VitrineBundle\Entity\Panier;
use sil29\VitrineBundle\Entity\Commande;
use sil29\VitrineBundle\Entity\LigneCommande;

class PanierController extends Controller {
	public function contenuPanierAction() {
		$session = $this->getRequest ()->getSession ();
		$panier = $session->get ( 'Panier', new Panier () );
		$contenuPanier = $panier->getContenu ();
		
		return $this->render ( 'sil29VitrineBundle:Default:panier.html.twig', array (
				'montantPanier' => $panier->getMontantPanier (),
				'nombreArticles' => $panier->getNombreArticles (),
				'panier' => $panier 
		) );
	}
	public function ajouterPanierAction($idElementFacturable) {
		$session = $this->getRequest ()->getSession ();
		$panier = $session->get ( 'Panier', new Panier () );
		$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $idElementFacturable );
		$panier->ajoutArticle ( $elementFacturable );
		$session->set ( 'Panier', $panier );
		return $this->contenuPanierAction ();
	}
	public function supprimerPanierAction($idElementFacturable) {
		$session = $this->getRequest ()->getSession ();
		$panier = $session->get ( 'Panier' );
		if ($panier != null) {
			$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $idElementFacturable );
			if ($elementFacturable != null) {
				$panier->supprimerArticle ( $elementFacturable );
			}
		}
	}
	public function genererCartbarAction() {
		$session = $this->getRequest ()->getSession ();
		$panier = $session->get ( 'Panier' );
		if ($panier == null) {
			$panier = new Panier ();
		}
		return $this->render ( 'sil29VitrineBundle:Default:cartbar.html.twig', array (
				'montantPanier' => $panier->getMontantPanier (),
				'nombreArticles' => $panier->getNombreArticles () 
		) );
	}
	public function validerPanierAction() {
		// Creation de la commande
		$commande = new Commande ();
		$commande->setDateCommande ( new \DateTime () );
		// - Récupération de l'utilisateur à partir de son id
		
		$user = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:User' )->find ( $this->getRequest ()->getSession ()->get ( 'idUser' ) );
		$commande->setUser ( $user );
		$commande->setStatut ( 0 );
		// récupération du panier en session
		$panier = $panier = $this->getRequest ()->getSession ()->get ( 'Panier' );
		$contenu = $panier->getContenu ();
		// creation du contenu de la commande
		
		$em = $this->getDoctrine ()->getManager ();
		
		foreach ( $contenu as $ligneAffaire ) {
			$ligneCommande = new LigneCommande ();
			$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $ligneAffaire [0]->getId () );
			$ligneCommande->setElementFacturable ( $elementFacturable );
			$ligneCommande->setQuantite ( $ligneAffaire [1] );
			$em->persist ( $ligneCommande );
			$em->flush ();
			$commande->addLignesCommande ( $ligneCommande );
		}
		
		// sauvegarde de la commande
		$em->persist ( $commande );
		$em->flush ();
		
		// vidage du panier
		$this->getRequest ()->getSession ()->remove ( 'Panier' );
		// affichage de la vue
		return $this->render ( 'sil29VitrineBundle:Default:validationCommande.html.twig', array (
				'commande' => $commande 
		) );
	}
	
	public function genererFactureAction($id) {
		$commande = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:Commande' )->find($id);
		$html = $this->renderView('sil29VitrineBundle:Facture:factureTemplate.pdf.twig', array('commande' => $commande));
		
		return new Response(
				$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
				200,
				array(
						'Content-Type'          => 'application/pdf',
						'Content-Disposition'   => 'attachment; filename="file.pdf"'
				)
		);
	}
	
	public function augmenterQuantiteAction(){
		$session = $this->getRequest ()->getSession ();
		$ligne = $this->getRequest()->get('ligne');		
		// récupération du panier en session
		$panier = $this->getRequest ()->getSession ()->get ( 'Panier' );
		$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $ligne );
		
		$contenuPanier = $panier->getContenu ();
		$panier->ajoutArticle ( $elementFacturable );
		$session->set ( 'Panier', $panier );
		return $this->contenuPanierAction ();
		
	}
	
	public function diminuerQuantiteAction() {
		$session = $this->getRequest ()->getSession ();
		$ligne = $this->getRequest()->get('ligne');
		// récupération du panier en session
		$panier = $this->getRequest ()->getSession ()->get ( 'Panier' );
		$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $ligne );
		
		$panier->diminuerArticle ( $elementFacturable );
		$session->set ( 'Panier', $panier );
		return $this->contenuPanierAction ();
	}
	
	public function supprimerArticleAction(){
		$session = $this->getRequest ()->getSession ();
		$ligne = $this->getRequest()->get('ligne');
		// récupération du panier en session
		$panier =  $this->getRequest ()->getSession ()->get ( 'Panier' );
		$elementFacturable = $this->getDoctrine ()->getManager ()->getRepository ( 'sil29VitrineBundle:ElementFacturable' )->find ( $ligne );
		
		$panier->supprimeArticle ( $elementFacturable );
		$session->set ( 'Panier', $panier );
		return $this->contenuPanierAction ();
	}
}